package dataAccess;

import java.sql.*;
import java.util.*;

import dataAccess.Connector;
import dataAccess.DALException;
import dataAccess.RentDTO;

public class MySQLRentDAO implements RentDAO{

	public RentDTO getRent(int rentID) throws DALException {
		ResultSet rs = Connector.doQuery("SELECT * FROM Rent WHERE Rent_ID = "
				+ rentID);
		try {
			if (!rs.first())
				throw new DALException("Lejen " + rentID + " findes ikke");
			return new RentDTO(rs.getInt("Rent_ID"), rs.getInt("Field_ID"),
					rs.getInt("BaseRent"), rs.getInt("1House"),
					rs.getInt("2Houses"), rs.getInt("3Houses"),
					rs.getInt("4Houses"), rs.getInt("Hotel"));
		} catch (SQLException e) {
			throw new DALException(e);
		}
	}

	public List<RentDTO> getRentList() throws DALException {
		List<RentDTO> data = new ArrayList<RentDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM Rent");
		try {
			while (rs.next()) {
				data.add(new RentDTO(rs.getInt("Rent_ID"), rs
						.getInt("Field_ID"), rs.getInt("BaseRent"), rs
						.getInt("1House"), rs.getInt("2Houses"), rs
						.getInt("3Houses"), rs.getInt("4Houses"), rs
						.getInt("Hotel")));
			}
		} catch (SQLException e) {
			throw new DALException(e);
		}
		return data;
	}

	public void createRent(RentDTO rent) throws DALException {
		Connector.doUpdate("INSERT INTO Rent VALUES (" + rent.getRentID()
				+ ", " + rent.getFieldID() + ", " + rent.getBaseRent() + ", "
				+ rent.getOneHouse() + ", " + rent.getTwoHouses() + ", "
				+ rent.getThreeHouses() + ", " + rent.getFourHouses() + ", "
				+ rent.getHotel() + ");");
	}

	public void updateRent(RentDTO rent) throws DALException {
		Connector.doUpdate("UPDATE Rent SET Field_ID = " + rent.getFieldID()
				+ ", BaseRent = " + rent.getBaseRent() + ", 1House = "
				+ rent.getOneHouse() + ", 2Houses = " + rent.getTwoHouses()
				+ ", 3Houses = " + rent.getThreeHouses() + ", 4Houses = "
				+ rent.getFourHouses() + ", Hotel = " + rent.getHotel()
				+ " WHERE Rent_ID = " + rent.getRentID() + ";");
	}

}
